package jml.def;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Properties;

import jml.config.Config;
import jml.defaultimple.util.UFile;


public abstract class Plugin
{	
	private String name;
	private Label pluginLabel;
	private Filter pluginFilter;
	
	private Hashtable<String, String> parameters;
	
	// hooks para notificar sobre la ocurrencia de eventos
	public abstract void onLoad(Title t);
	public abstract void onPlay(Title t);
	public abstract void onEdit(Title t);
	public abstract void onRefresh(Library t);
	public abstract void onOpenLabel(String filter,String label);
	public abstract void beforeLoading(Library lib);
	public abstract void afterLoading(Library lib);
	
	public String getName()
	{
		return name;
	}
	
	public String getAttribute(String attname)
	{
		FileInputStream fis = null;
		try
		{
			// folder donde grabar el att
			String folder = Config.getConfig(false).getPlugins().getStorage();
			
			// si no existe la carpeta la creo
			UFile.createFolderIfNotExists(folder);
			
			String propFilename=folder+"/"+getClass().getName()+".properties";
			UFile.createFileIfNoExists(propFilename);
			
			// leo las propiedades
			Properties p = new Properties();
			fis= new FileInputStream(propFilename);
			p.load(fis);
			fis.close();
			
			return p.getProperty(attname);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}		
	
	public void setAttribute(String attname, String attvalue)
	{
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try
		{
			// folder donde grabar el att
			String folder = Config.getConfig(false).getPlugins().getStorage();
			
			// si no existe la carpeta la creo
			UFile.createFolderIfNotExists(folder);
			
			String propFilename=folder+"/"+getClass().getName()+".properties";
			UFile.createFileIfNoExists(propFilename);
			// leo las propiedades
			Properties p = new Properties();
			fis= new FileInputStream(propFilename);
			p.load(fis);
			fis.close();
			
			p.put(attname, attvalue);
			
			fos = new FileOutputStream(propFilename);
			p.store(fos, "--- NO TOCAR ---");
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	
	// retorna en label (solapa de acordion) del plugin
	public Label getPluginLabel()
	{
		return pluginLabel;
	}
	
	public void setName(String n)
	{
		name = n;
	}
	
	public void setPluginLabel(Label lbl)
	{
		pluginLabel = lbl;
	}
		
	public void setParameters(Hashtable<String, String> parameters)
	{
		this.parameters = parameters;
	}
	public Hashtable<String, String> getParameters()
	{
		return parameters;
	}
	public void setPluginFilter(Filter pluginFilter)
	{
		this.pluginFilter = pluginFilter;
	}
	public Filter getPluginFilter()
	{
		return pluginFilter;
	}

}